/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package client;
import entreprise.Drug;
import entreprise.DrugService;
import entreprise.Patient;
import entreprise.PatientService;
import entreprise.Service;
import entreprise.ServiceService;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import entreprise.Session;
import entreprise.SessionService;
import java.util.Date;
/**
 *
 * @author duonghau
 */
public class SessionBean {
    private String sessionID="";
    private Date sessionDate=null;
    private String doctor="";
    private String require="";
    private String diagnostic="";
    private String patientID="";
    private String message;
    private String messageStyle;
    private String patientName="";
    ArrayList<Drug> drugs;
    ArrayList<Service> services;
    public String[] selectedDrugs;
    public String[] selectedServices;
    public SessionBean() {
        
    }

    public String getSessionID() {
        return sessionID;
    }

    public void setSessionID(String sessionID) {
        this.sessionID = sessionID;
    }

    public Date getSessionDate() {
        return sessionDate;
    }

    public void setSessionDate(Date sessionDate) {
        this.sessionDate = sessionDate;
    }

    public String getDoctor() {
        return doctor;
    }

    public void setDoctor(String doctor) {
        this.doctor = doctor;
    }

    public String getRequire() {
        return require;
    }

    public void setRequire(String require) {
        this.require = require;
    }

    public String getDiagnostic() {
        return diagnostic;
    }

    public void setDiagnostic(String diagnostic) {
        this.diagnostic = diagnostic;
    }

    public String getPatientID() {
        return patientID;
    }

    public void setPatientID(String patientID) {
        this.patientID = patientID;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessageStyle() {
        return messageStyle;
    }

    public void setMessageStyle(String messageStyle) {
        this.messageStyle = messageStyle;
    }

    public String getPatientName() {
        return patientName;
    }

    public ArrayList<Drug> getDrugs() {
        DrugService drugService=new DrugService();
        drugs=drugService.drugs("", 0, 100);
        return drugs;
    }
    
    public ArrayList<Drug> getDrugsOfSession(){
        SessionService sessionService=new SessionService();
        drugs=sessionService.getDrugsOfSession(Integer.parseInt(sessionID));
        return drugs;
    }
    
    public ArrayList<Service> getServiceOfSession(){
        SessionService sessionService=new SessionService();
        services=sessionService.getServicesOfSession(Integer.parseInt(sessionID));
        return services;
    }
    
    public void setDrugs(ArrayList<Drug> drugs) {
        this.drugs = drugs;
    }

    public ArrayList<Service> getServices() {
        ServiceService serviceService =new ServiceService();
        services=serviceService.services("", 0, 100);
        return services;
    }

    public void setServices(ArrayList<Service> services) {
        this.services = services;
    }    

    public String[] getSelectedDrugs() {
        return selectedDrugs;
    }

    public void setSelectedDrugs(String[] selectedDrugs) {
        this.selectedDrugs = selectedDrugs;
    }

    public String[] getSelectedServices() {
        return selectedServices;
    }

    public void setSelectedServices(String[] selectedServices) {
        this.selectedServices = selectedServices;
    }
    
    public void getPatientInfo(){
        PatientService patientService=new PatientService();
        Patient patient=patientService.getPatient(patientID);
        if(patient!=null){
            patientName=patient.getPatientName();
        }
    }
    
    public ArrayList<Session> getSessions(){
        ArrayList<Session> sessions=new ArrayList<>();
        if(!patientID.equals("")){
            SessionService sessionService=new SessionService();
            sessions=sessionService.sessions(patientID);
        }
        return sessions;
    }
    
    public void getSessionDetail(){
        if(!sessionID.equals("")){
            SessionService sessionService=new SessionService();
            Session session=sessionService.getSession(sessionID);
            if(session!=null){
                sessionDate=session.getSessionDate();
                doctor=session.getDoctor();
                require=session.getRequire();
                diagnostic=session.getRequire();
                patientID=session.getPatientID();
            }
            getPatientInfo();
        }
    }
    
    public String addSession(){
        SessionService sessionService=new SessionService();
        if(sessionService.createSession(sessionID, sessionDate, doctor, require, diagnostic, patientID)){
            int sessionID=sessionService.getLatestSessionID();
            sessionService.createSessionDrug(sessionID, selectedDrugs);
            sessionService.createSessionService(sessionID,selectedServices);
            message = "Session add success";
            messageStyle = "success";               
        }else{
            message="Session add error";
            messageStyle="error";            
        }
        return "patient.xhtml?faces-redirect=true&patientID="+patientID+"&message="+message+"&messageStyle="+messageStyle;
    }
    public String editSession(){
        SessionService sessionService=new SessionService();
        if(sessionService.updateSession(sessionID, sessionDate, doctor, require, diagnostic, patientID)){
            message="Session update success";
            messageStyle="success";             
        }else{
            message="Session update error";
            messageStyle="error";              
        }
        return "patient.xhtml?faces-redirect=true&patientID="+patientID+"&message="+message+"&messageStyle="+messageStyle;
    }
    public String deleteSession(){
        SessionService sessionService=new SessionService();
        if(sessionService.deleteSession(sessionID)){
            message="Session delete success";
            messageStyle="success";              
        }else{
            message="Session delete error";
            messageStyle="error";              
        }
        return "patient.xhtml?faces-redirect=true&patientID="+patientID+"&message="+message+"&messageStyle="+messageStyle;
    }
}
